const App = getApp();
import drawQrcode from '../../utils/weapp.qrcode.esm.js'
import siteinfo from '../../siteinfo';
Page({
  data: {
    dataType: 'all',
    timeType: 'all',
    list: {},
    getList: [],
    is_fw: false,
    order_id: 0,
    radio: '1',
    scrollHeight: null, // 列表容器高度
    page: 1,
    last_page: 1,
    sk_img: '',
    drawQrcode: '',
    canvasId: 'myQrcode',
    trans: false,
    trans_id: '',
    transfer: [],
    checked: '',
    timer: ''
  },
  Gbsk: function (e) {
    this.setData({
      sk_img: '',
      drawQrcode: ''
    })
    this.getOrder()
    clearInterval(this.data.timer);
  },
  ShouKuan: function (e) {
    let _this = this;
    if (e.target.dataset.price == '0.00') {
      wx.request({
        url: siteinfo.siteroot + 'index.php?s=' + '/wechat/pay/serverButton',
        data: {
          order_id: e.target.id,
        },
        header: {
          'content-type': 'application/json'
        },
        success: (res) => {
          if (res.data.code == 1) {
            wx.showToast({
              title: '确认成功',
            })
            _this.getOrder()
          }
        }
      })
    } else {
      console.log(_this.data.canvasId)
      console.log(_this.data.drawQrcode)
      // App._post_form('service/getPaycode', {}, function (result) {
      //   _this.setData({
      //     sk_img: result.data.payment_code
      //   })
      // });
      _this.setData({
          drawQrcode: siteinfo.siteroot + 'index.php?s=' + '/wechat/pay/index&order_id=' + e.target.id,
          canvasId: _this.data.canvasId + 1
        }),
        drawQrcode({
          width: 200,
          height: 200,
          canvasId: _this.data.canvasId,
          text: siteinfo.siteroot + 'index.php?s=/wechat/pay/index&order_id=' + e.target.id
          // text: 'https://jiazheng.bigchun.com/index.php?s=/wechat/pay/index&order_id=' + e.target.id
          // text: 'https://aijia.bigchun.com/index.php?s=/wechat/pay/index&order_id=' + e.target.id
        })
      _this.data.timer = setInterval(function () {
        App._post_form('appointment/getOfflinePayStatus', {
          id: e.currentTarget.id
        }, function (result) {
          console.log(result)
          if (result.data.status == 1) {
            wx.showToast({
              title: '支付成功',
            })
            clearInterval(_this.data.timer);
            _this.Gbsk()
            _this.getOrder()
          } else {
            // _this.getOrder()
          }

        });
      }, 10000)
    }
  },
  GuanZhu: function (e) {
    console.log(e)
    let _this = this;
    App._post_form('wxevent/QRCode', {
      order_id: e.target.id
    }, function (result) {
      _this.setData({
        sk_img: result.url
      })
    });
  },

  order_transfer: function (e) {
    console.log(e)
    let _this = this
    App._post_form('appointment/serviceSon', {
      id: e.currentTarget.id
    }, function (result) {
      console.log(result.data)
      _this.setData({
        transfer: result.data,
        trans: true,
        trans_id: e.currentTarget.id
      })
    });
  },
  radioChange: function (e) {
    var _this = this;
    _this.setData({
      checked: e.detail.value,
    })
  },
  affirm: function () {
    let _this = this
    if (_this.data.checked != '') {
      console.log(_this.data.checked)
      var obj = _this.data.transfer.find(function (id) {
        console.log(id)
        return id.user_id == _this.data.checked
      })
      console.log(obj.real_name)
      let name = obj.real_name
      App._post_form('appointment/takeServerSon', {
        id: _this.data.trans_id,
        user_id: _this.data.checked,
        user_name: name
      }, function (result) {
        console.log(result)
        if (result.code == 1) {
          _this.setData({
            trans: false
          })
          wx.showToast({
            title: '转单成功',
          })
          _this.getOrder()

        } else {
          wx.showToast({
            title: '转单失败',
          })
        }

      });
    }
  },
  exit: function () {
    let _this = this
    _this.setData({
      trans: false
    })
  },
  clickNum: function (e) {
    var timeType = e.currentTarget.dataset.type
    this.setData({
      timeType: timeType
    })
    this.getOrder()
  },
  makePhoneCall: function (e) {
    var phone = e.currentTarget.dataset.phone;
    var id = e.currentTarget.dataset.id
    console.log(e)
    wx.makePhoneCall({
      phoneNumber: phone,
      success:function(e){
        if(e.errMsg == "makePhoneCall:ok"){
          //YPP 插入日志
          App._post_form('appointment/makephonecall', {
             id:id
          }, function (result) {
            console.log(result)
          });
        }
      },
      fail:function(e){
        
      }
    })
  },
  end_fuwu: function () {
    this.setData({
      is_fw: false,
      order_id: 0
    });
  },
  SubmitFuwu: function (e) {
    var order_id = this.data.order_id
    if (!order_id) {
      wx.showModal({
        title: '温馨提示：',
        content: '参数错误',
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {

        }
      })
      return false
    }
    let _this = this;
    App._post_form('service/startService', {
      order_id: order_id
    }, function (result) {
      _this.setData({
        order_id: 0,
        is_fw: false
      })
      wx.showModal({
        title: '温馨提示：',
        content: result.msg,
        showCancel: false,
        confirmText: '关闭提示',
        success(res) {
          _this.getOrder()
        }
      })
    });
  },
  Start_fuwu: function (e) {
    console.log(e)
    var order_id = e.currentTarget.id
    this.setData({
      is_fw: true,
      order_id: order_id
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(siteinfo.siteroot)
    var dataType = options.type
    this.setListHeight();
    if (dataType) {
      this.setData({
        dataType: dataType
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  getOrder(isPage, page) {
    let _this = this;
    var dataType = _this.data.dataType
    var timeType = _this.data.timeType
    console.log(_this.data.dataType, _this.data.timeType)
    App._post_form('service/getList', {
      page: page || 1,
      dataType: _this.data.dataType,
      timeType: timeType
    }, result => {
      let resList = result.data.list.data,
        dataList = _this.data.getList;
      var last_page = result.data.list.last_page
      if (isPage == true) {
        _this.setData({
          getList: dataList.concat(resList),
          last_page: last_page
        });
      } else {
        _this.setData({
          getList: resList,
          last_page: last_page,
        });
      }
    });
    App._post_form('service/getCountList', {
      dataType: _this.data.dataType,
    }, result => {
      let list = result.data.list;
      _this.setData({
        list: list
      });
    });

  },
  bindHeaderTap: function (e) {
    console.log(e.currentTarget.dataset)
    var dataType = e.currentTarget.dataset.type
    this.setData({
      dataType: dataType,
      timeType: 'all'
    })
    this.getOrder(false, 1)
  },
  onShow: function () {
    this.getOrder(false, 1)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },
  setListHeight() {
    let systemInfo = wx.getSystemInfoSync(),
      rpx = systemInfo.windowWidth / 750, // 计算rpx
      tapHeight = Math.floor(rpx * 88), // tap高度
      scrollHeight = systemInfo.windowHeight - tapHeight; // swiper高度
    this.setData({
      scrollHeight
    });
  },
  bindDownLoad() {
    var last_page = this.data.last_page
    if (this.data.page >= last_page) {
      return false;
    }
    this.getOrder(true, ++this.data.page);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})